//
//  LAGAppMapView.m
//  LAMapBasicDemo
//
//  Created by 罗亮富 on 2017/12/5.
//  Copyright © 2017年 2Bulu. All rights reserved.
//

#import "LAGAppMapView.h"
#import <MapBasics/MapBasics.h>


@implementation LAGAppMapView
{
    __weak MKTileOverlay *_contentLayer;
}

-(void)setContentSource:(LAMapSource *)contentSource
{
    if([_contentSource isEqual:contentSource])
        return;
    
    [self removeOverlay:_contentLayer];
    
    LAGTileOverlay *tile = [[LAGTileOverlay alloc] initWithURLTemplate:contentSource.tileUrlTemplate];
    tile.canReplaceMapContent = YES;
    tile.tileSize = CGSizeMake(contentSource.tileSize, contentSource.tileSize);
    [self insertOverlay:tile atIndex:0];
    _contentLayer = tile;
    
    if(_contentSource)
    {
        [self refreshMapGemotriesFromCoordinateSystem:_contentSource.coordinateSystem toCoordinateSystem:contentSource.coordinateSystem];
    }
    
    _contentSource = contentSource;
    
}

//switch annotations polylines and overlays between different coordinate system
-(void)refreshMapGemotriesFromCoordinateSystem:(LACoordinateSystem)preCordSys toCoordinateSystem:(LACoordinateSystem)curCordSys
{
    if(preCordSys == curCordSys)
        return;
    
#warning to be finished
}

#if 0
#pragma mar- overlay<->sourceMapView relationship manage
-(void)assosicateOvlerLay:(TrackPolyline *)trackLine
{
    //保存trackLine对mapview的引用,因为一条trackLine可以添加到多个地图实例，所以这里是一个集合
    NSMutableDictionary *mDic = [trackLine bindedObjectForKey:kLineMapRenderedSources];
    if(!mDic)
    {
        mDic = [NSMutableDictionary dictionaryWithCapacity:3];
        [trackLine bindObject:mDic forKey:kLineMapRenderedSources policy:ObjectBindStrong];
    }

    [mDic setObject:[WeakableRefObject instanceWithRefrenceObject:self] forKey:[NSString stringWithFormat:@"mapSrc%p",self]];
}

-(void)unAssosicateOvlerLay:(TrackPolyline *)trackLine
{
    NSMutableDictionary *mDic = [trackLine bindedObjectForKey:kLineMapRenderedSources];
    if(mDic)
    {
        NSMutableArray *mkeys = [NSMutableArray arrayWithCapacity:mDic.count];
        [mDic enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            
            WeakableRefObject *weakRef =  (WeakableRefObject *)obj;
            if(weakRef.refrenceObject == self)
                [mkeys addObject:key];
            
        }];
        [mDic removeObjectsForKeys:mkeys];
    }
}

-(void)addOverlay:(id<MKOverlay>)overlay
{
    [super addOverlay:overlay];
    
    if([overlay isKindOfClass:[TrackPolyline class]])
        [self assosicateOvlerLay:(TrackPolyline *)overlay];
}

-(void)addOverlays:(NSArray<id<MKOverlay>> *)overlays
{
    [super addOverlays:overlays];
    
    NSArray *ovls = [overlays copy];
    for(id<MKOverlay> overlay in ovls)
    {
        if([overlay isKindOfClass:[TrackPolyline class]])
            [self assosicateOvlerLay:(TrackPolyline *)overlay];
    }
}

-(void)removeOverlay:(id<MKOverlay>)overlay
{
    [super removeOverlay:overlay];
    if([overlay isKindOfClass:[TrackPolyline class]])
        [self unAssosicateOvlerLay:(TrackPolyline *)overlay];
}

-(void)removeOverlays:(NSArray<id<MKOverlay>> *)overlays
{
    [super removeOverlays:overlays];
    
    NSArray *ovls = [overlays copy];
    for(id<MKOverlay> overlay in ovls)
    {
        if([overlay isKindOfClass:[TrackPolyline class]])
            [self unAssosicateOvlerLay:(TrackPolyline *)overlay];
    }
}
#endif
@end


